<?php

require_once dirname(__FILE__) . '/../interfaces/node.interface.php';

abstract class JS_Node implements Node {
	
	protected $val;
	protected $parent;
	protected $children;
	protected $count;
	
	public function __construct(&$val=null) {
		$this->val = $val;
		$this->count = 0;
		$this->parent = null;
		$this->children = array();
	}
	
	public function &getValue() {
		return $this->val;
	}
	
	public function &getParent() {
		return isset($this->parent) ? $this->parent : null;
	}
	
	public function &getChild($i) {
		//return isset($this->children[$i]) ? $this->children[$i] : ($node = new JS_Node());
		return $this->children[$i];
	}
	
	public function &getChildren() {
		return $this->children;
	}
	
	public function getNodeType() {
		return get_class($this);
	}
	
	public function getNumChildren() {
		return $this->count;
	}
	
	public function addChild(Node &$node=null) {
		if ($node) {
			$this->children[$this->count++] = $node;
			$node->setParent($this);
		}
	}
	
	public function setParent(Node &$parent=null) {
		$this->parent = $parent;
	}
	
	public function interpret() {
		return $this->val;
	}
	
	public function __toString() {
		return $this->val . '';
	}
	
}

?>